package com.siruier.boss.ui.activity.mall

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.kingja.loadsir.core.LoadService
import com.siruier.boss.R
import com.siruier.boss.api.core.launchUI
import com.siruier.boss.api.core.toData
import com.siruier.boss.api.mallApi
import com.siruier.boss.bean.ShopAddressBean
import com.siruier.boss.databinding.ActivityReceivingAddressBinding
import com.siruier.boss.databinding.ItemReceivingAddressBinding
import com.siruier.boss.ui.api.ApiObserver
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.base.BaseRecyclerAdapter
import com.siruier.boss.ui.ext.showEmptyCallback
import com.siruier.boss.ui.ext.showLoadingCallback
import com.siruier.boss.ui.helper.InjectBundle
import com.siruier.boss.ui.helper.loadsir.LoadSirHelper
import com.siruier.boss.ui.helper.newInstance
import com.siruier.boss.ui.listener.OnStartActivityListener

class ReceivingAddressActivity : BaseActivity<ActivityReceivingAddressBinding>(), View.OnClickListener {
    companion object {
        const val RECEIVING_ADDRESS = "RECEIVING_ADDRESS"
        const val CLICK_FINISH = "mClickFinish"
    }

    private val mClickFinish: Boolean by InjectBundle()
    private val mLoadService: LoadService<*> by lazy { LoadSirHelper.register(vb.rvReceivingAddress) { onReload() } }
    private val mReceivingAddressAdapter = ReceivingAddressAdapter()

    override fun initView() {
        bindViewClick(vb.buttonAddReceivingAddress)
        vb.rvReceivingAddress.adapter = mReceivingAddressAdapter
        vb.rvReceivingAddress.layoutManager = LinearLayoutManager(this)
        mReceivingAddressAdapter.setOnClickAdapterListenter { view, i ->
            if (view.id == R.id.ivEdit || !mClickFinish) {
                launchStartActivityForResult(ReceivingAddressAddOrEditActivity::class.java, options = {
                    putParcelable("mShopAddressBean", mReceivingAddressAdapter.getItem(i))
                }) { onReload() }
            } else {
                setResult(Activity.RESULT_OK, Intent().apply { putExtra(RECEIVING_ADDRESS, mReceivingAddressAdapter.getItem(i)) })
                finish()
            }
        }
    }

    override fun initData() {
        onReload()
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.buttonAddReceivingAddress -> {
                launchStartActivityForResult(ReceivingAddressAddOrEditActivity::class.java) { onReload() }
            }
        }
    }

    private fun onReload() {
        launchUI({ mallApi.addressList().toData() }, ApiObserver(loadService = mLoadService) {
            it?.run {
                if (isEmpty()) {
                    mLoadService.showEmptyCallback()
                } else {
                    mLoadService.showSuccess()
                    mReceivingAddressAdapter.datas = this
                    mReceivingAddressAdapter.notifyDataSetChanged()
                }
            }
        })
    }

    class ReceivingAddressAdapter : BaseRecyclerAdapter<ItemReceivingAddressBinding, ShopAddressBean>() {
        override fun onBindViewHolder(vb: ItemReceivingAddressBinding, bean: ShopAddressBean, position: Int) {
            vb.run {
                bean.run {
                    tvName.text = name
                    tvPhone.text = phone
                    tvAddress.text = province + city + county + address
                    viewDefault.isVisible = deful == "1"
                    setViewClick(position, root, ivEdit)
                }
            }
        }
    }
}
